"""
__author__ : shanhai
__time__   :2024/4/3 12:36
@Project   :bzy-automated-testing
@File      :order_entry_page.py

订单录入页面
"""
import allure
from selenium.common import NoSuchElementException
from selenium.webdriver.common.by import By

from page.home_page import HomePage
from utils.logger import logger


class OrderEntryPage(HomePage):
    _MANUAL_CREATE = (By.XPATH, "//*[text()='手动创建']")
    _CHANNEL = (By.CSS_SELECTOR, '[placeholder="请选择所属渠道"]')
    _渠道 = (By.XPATH, "//span[text()='自动化测试_渠道']")
    _CONTENT = (By.CSS_SELECTOR, "textarea")
    _IDENTIFY = (By.CSS_SELECTOR, ".el-button--small")
    _DATA = (By.CSS_SELECTOR, ".el-table__row span")
    _商品框 = (By.CSS_SELECTOR, "[placeholder='请选择']")
    _NUMS = (By.CSS_SELECTOR, "[type='number']")
    _详细地址 = (By.CSS_SELECTOR, '[placeholder="详细地址"]')
    _省市区 = (By.CSS_SELECTOR, '[placeholder="省市区"]')
    # 会有多个元素，下表1是收件人，2是手机号
    _识别结果 = (By.XPATH, "//*[@class='el-input__inner']")
    _COMMIT = (By.XPATH, "//*[text()='全部提交']")

    @allure.step("点击手动创建")
    def click_manual_create(self):
        logger.info("点击手动创建")
        self.find_and_click(*self._MANUAL_CREATE)
        return self

    @allure.step("填写订单信息")
    def create_order(self, order_data: str):
        logger.info("选择渠道")
        self.find_and_click(*self._CHANNEL)
        self.find_and_click(*self._渠道)
        # 输入订单信息
        self.find_and_send(order_data, *self._CONTENT)
        # 识别
        self.find_and_click(*self._IDENTIFY)
        self.set_time(1)
        return self

    @allure.step("不选择渠道")
    def no_channel(self, order_data: str):
        # 输入订单信息
        self.find_and_send(order_data, *self._CONTENT)
        # 识别
        self.find_and_click(*self._IDENTIFY)
        self.set_time(1)
        return self

    @allure.step("判断识别的结果，以及选择商品")
    def recognition(self, num: int, sp: str, goods_name='测试商品（勿删）'):
        # 识别后的结果
        self.eles = self.finds(*self._DATA)
        name = self.eles[1].text
        phone = self.eles[2].text
        address = self.eles[3].text
        self.activation()
        # 选择商品
        self.select_goods(num, goods_name, sp)
        result_list = [name, phone, address]
        logger.info(f"识别后的结果为：{name, phone, address}")
        return result_list, self

    def activation(self):
        self.set_time(1)
        # 点击下方识别结果，激活输入操作
        self._driver.execute_script('document.querySelector(".table-data-cell.row-invalid.col-invalid").click()')
        return

    @allure.step("选择商品信息")
    def select_goods(self, num: int, goods_name: str, sp: str):
        """
        :param num: 商品数量
        :param goods_name: 商品名称
        :param sp: 商品规格
        :return:
        """
        logger.info("选择商品")
        商品 = (By.XPATH, f"//span[contains(text(), '{goods_name}')]")
        规格 = (By.XPATH, f"//span[text()='{sp}']")
        self.find_and_send(goods_name, *self._商品框)
        self.set_time(1)
        self.find_and_click(*商品)
        self.find_and_click(*规格)
        设置供货价弹窗 = (By.XPATH, "//*[@class='el-dialog__body']")
        try:
            self.find(*设置供货价弹窗)
            logger.info("需要设置供货价")
            self.find_and_send('12', By.XPATH, "//*[@class='el-input-number']//input")
            弹窗提交 = (By.CSS_SELECTOR, ".el-dialog__footer .el-button.el-button--primary")
            self.find_and_click(*弹窗提交)
            close = (By.CSS_SELECTOR, ".el-message-box__close.el-icon-close")
            self.find_and_click(*close)
        except NoSuchElementException:
            self.set_time(1)
        self.activation()
        self.find(*self._NUMS).send_keys(num)
        return

    @allure.step("提交")
    def commit(self):
        logger.info("提交")
        self.set_time(1)
        self.find_and_click(*self._COMMIT)
        return self

    def get_submit_error_message(self):
        error = (By.XPATH, "//*[contains(text(),'未通过校验')]")
        try:
            self.find(*error)
        except:
            return True
        else:
            return False

    def get_error_message(self):
        self.set_time(1)
        m = self._driver.execute_script('return document.querySelector(".el-message.el-message--error").textContent')
        return m

    def commit_error(self):
        _弹窗 = (By.XPATH, "//*[@class='el-message el-message--error']//span")
        return self.finds(*_弹窗)[1].text

    def get_warning_message(self):
        self.set_time(1)
        m = self._driver.execute_script('return document.querySelector(".el-message.el-message--warning").textContent')
        return m

    @allure.step("获取提示信息")
    def get_prompt_information(self):
        self.set_time(0.5)
        text = self._driver.execute_script('return document.querySelector(".el-message__content").textContent')
        return text
